#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./trf-4.09.1.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

wget -t 10 -c -P $buildpath https://github.com/Benson-Genomics-Lab/TRF/archive/v4.09.1.tar.gz
tar xf $buildpath/v4.09.1.tar.gz -C $buildpath
cd $buildpath/TRF-4.09.1
mkdir -p build && cd build
../configure && make  -j$(nproc) CC=clang
mkdir $installpath/trf && cp -r ./src/trf $installpath/trf/trf
cat>"$installpath/trf/trf_modulefiles"<<EOF
#%Module1.0
conflict trf
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set trf \$pwd
setenv trf \$trf
prepend-path PATH \$trf/
EOF
